﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.NhomHangHoa
{
    public partial class frmQuanLy : Form
    {
        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        #region Delegate form xử lý
        public void GetValue(Boolean capnhat)
        {
            if (capnhat == true)
            {
                if (GUI.NhomHangHoa.frmXuLy.CheckClose == true)
                {
                    Them();
                }
                GUI.NhomHangHoa.frmXuLy.CheckClose = false;
                HienThi();
            }
        }
        #endregion

        #region Khởi tạo
        private Entities.NhomHangHoa[] DanhSach;
        private Entities.NhomHangHoa NhomHangHoa = null;
        private string MaNhomHangHoa = string.Empty;
        private Boolean CheckClose = false;

        public frmQuanLy()
        {
            InitializeComponent();
        }
        #endregion

        #region Check Permission
        public bool CheckPer(int hanhDong)
        {
            return Common.Utilities.Check(Common.FormName.Quanlynhomhanghoa, hanhDong);
        }
        #endregion

        #region Thêm
        private void Them()
        {
            try
            {
                GUI.NhomHangHoa.frmXuLy frm = new GUI.NhomHangHoa.frmXuLy("Them", this.NhomHangHoa);
                frm.GetData = new GUI.NhomHangHoa.frmXuLy.GetString(GetValue);
                
                frm.Show();
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Sửa
        private void Sua()
        {
            try
            {
                if (this.NhomHangHoa == null)
                {
                    MessageBox.Show("Bạn phải chọn nhân viên.", "Thông báo");
                    return;
                }
                GUI.NhomHangHoa.frmXuLy frm = new GUI.NhomHangHoa.frmXuLy("Sua", this.NhomHangHoa);
                frm.GetData = new GUI.NhomHangHoa.frmXuLy.GetString(GetValue);
                
                frm.Show();
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        #endregion

        #region Doanh thu
        private void DoanhThu()
        {
            try
            {
                MessageBox.Show("Chưa làm");
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Processing
        //lay ban ghi de sua hoac xem
        private void GetRow()
        {
            try
            {
                if (DanhSach == null) { return; }
                for (int i = 0; i < this.DanhSach.Length; i++)
                {
                    if (DanhSach[i].MaNhomHang == this.MaNhomHangHoa)
                    {
                        NhomHangHoa = DanhSach[i];
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Xóa
        private void Xoa()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn xóa  không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        Entities.NhomHangHoa nv = new Entities.NhomHangHoa();
                        nv.HanhDong = "Update";
                        nv.MaNhomHang = this.MaNhomHangHoa;
                        int k = Common.StaticVariable.InterfaceObject.DeletedNhomHangHoa(nv);
                        if (k != 0)
                        {
                            this.HienThi();
                        }
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Thoát
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        this.CheckClose = true;
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Event
        private void frmQuanLyNhomHangHoa_Load(object sender, EventArgs e)
        {
            this.HienThi(); this.WindowState = FormWindowState.Maximized;
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            Them();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            Sua();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            Xoa();
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        private void frmQuanLyNhomHangHoa_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (CheckClose == false)
                {
                    System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                    {
                        if (ketqua == System.Windows.Forms.DialogResult.Yes)
                        {
                            e.Cancel = false;
                        }
                        else
                        {
                            e.Cancel = true;
                        }
                    }
                }
                GetData(true);
            }
            catch (Exception)
            { }
        }
        #endregion

        #region HienThi
        private void HienThi()
        {
            try
            {
                Entities.NhomHangHoa nv = new Entities.NhomHangHoa();
                nv.HanhDong = "1";
                nv.MaNhomHang = "";
                // Common.StaticVariable.InterfaceObject cham' 1 cai se lay dc cac ham tu sql ma minh da tao trong interface
                DanhSach =  Common.StaticVariable.InterfaceObject.SelectNhomHangHoa(nv);
                dgvHienThi.DataSource = DanhSach;
                //hien thi lai cai gidview
                this.HienThiListview();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                DanhSach = null;
                HienThiListview();
            }
        }

        private void HienThiListview()
        {
            try
            {
                if (DanhSach == null)
                {
                    Entities.NhomHangHoa[] nv = new Entities.NhomHangHoa[0];
                    dgvHienThi.DataSource = nv;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.Columns["HanhDong"].HeaderText = "STT";
                dgvHienThi.Columns["HanhDong"].Visible = true;
                dgvHienThi.Columns["TenNhomHang"].HeaderText = "Tên nhóm hàng";
                dgvHienThi.Columns["TenNhomHang"].Visible = true;
                dgvHienThi.Columns["TenLoaiHang"].HeaderText = "Tên loại hàng";
                dgvHienThi.Columns["TenLoaiHang"].Visible = true;
                dgvHienThi.Columns["GhiChu"].HeaderText = "Ghi Chú";
                dgvHienThi.Columns["GhiChu"].Visible = true;
                if (dgvHienThi.RowCount > 0)
                {
                    Common.Utilities.CountDatagridview(dgvHienThi);
                    dgvHienThi.Focus();
                    dgvHienThi.Select();
                }
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        private void dgvHienThi_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvHienThi.Rows.Count <= 0)
            { return; }
            //lay ma nhan vien tren gidview
            MaNhomHangHoa = dgvHienThi.CurrentRow.Cells["MaNhomHang"].Value.ToString();
            this.GetRow();
        }

        private void dgvHienThi_DoubleClick(object sender, EventArgs e)
        {
            this.Sua();
        }

        #region Tìm
        public void TimKiem()
        {
            dgvHienThi.DataSource = (Entities.NhomHangHoa[])DanhSach.Where(p => p.MaNhomHang.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0).ToArray();
            HienThiListview();
        }
        #endregion

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            TimKiem();
        }

        private void btnTim_TextChanged(object sender, EventArgs e)
        {
            TimKiem();
        }

        private void btnchitiet_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.DanhSach.Length > 0)
                {
                    if (NhomHangHoa.MaNhomHang != null)
                    {
                        GUI.NhomHangHoa.frmChiTietNhomHang frm = new GUI.NhomHangHoa.frmChiTietNhomHang(NhomHangHoa.MaNhomHang);
                        frm.ShowDialog();
                    }
                    else
                    {
                        MessageBox.Show("Chọn nhóm hàng hóa", Common.Constants.GiaTriCacForm.HeThongCanhBao);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                if (this.DanhSach.Length > 0)
                {
                    MessageBox.Show("Chọn nhóm hàng hóa", Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    return;
                }
            }
        }
    }
}
